const router = require('koa-router')();
const tool = require("../tools/tool");
const biz = require("../tools/biz");
const {db, Op} = require("../models/db");
// 用户保存
router.post('/contact/save', async ctx => {
    let ret = await tool.save(ctx)
    try {
        let roomId = ctx.roomId
        if(roomId) {
            setTimeout(async () => {
                let list = await biz.todayKhs(ctx, tool.getDay())
                ctx.io.to(roomId).emit('serverUpdateKhs', list)
            }, 300)
        }
    } catch (e) {}
    return ret
})
router.post('/contact/myList', async ctx => {
    let tid = ctx.user.tenantId
    let uid = ctx.user.id
    let contactSql = ` (select * from contact where tenantId=${tid} and salesId=${uid} ) `
    ctx.request.body.order = ' order by salesId '
    let sql = `select * from (
                      select a.id,a.tenantId,a.categoryId,c.name categoryName,a.areaId,b.name areaName,a.salesId,d.name salesName,a.addressLocation,
                             a.name,a.linkMobile,a.linkName,a.linkPhone,a.address,a.remark,a.longitude,a.latitude,d.deptId   from ${contactSql} a left join
                             area b on a.areaId=b.id left join contact_cat c on a.categoryId=c.id 
                             left join admin d on a.salesId=d.id where a.tenantId=${tid} and a.isDel=0
                  ) a where tenantId=${tid} 
    `
    let ret = await tool.page(ctx, sql)
    ret.data.list.map(row => {
        row.lastBfDay = row.lastBfDay ? tool.dayDif('', row.lastBfDay) : ''
    })
    return ret
})

// 删除客户
router.post('/contact/delById', async ctx => {
    let ids = ctx.request.body.ids
    let c = await db.models['invoice'].count({
        where: {
            buId: {
                [Op.in]: ids
            }
        }
    })
    if(c > 0) {
        return  ctx.err('客户已使用,不能删除')
    }
    await db.models['contact'].destroy({
        where: {
            id: {
                [Op.in]: ids
            }
        }
    })
    return ctx.suc('已删除')
})
// 删除分类
router.post('/contactCat/delById', async ctx => {
    let ids = ctx.request.body.ids
    let c = await db.models['contact'].count({
        where: {
            categoryId: {
                [Op.in]: ids
            }
        }
    })
    if(c > 0) {
        return  ctx.err('分类已使用,不能删除')
    }
    await db.models['contactCat'].destroy({
        where: {
            id: {
                [Op.in]: ids
            }
        }
    })
    return ctx.suc('已删除')
})
// 客户分类
router.post('/contact/catList', async ctx => {
    let tid = ctx.user.tenantId
    let sql = `
        select * from contact_cat where tenantId=${tid} 
    `
    let ret = await tool.page(ctx, sql)
    return ret
})

// 客户管理
router.post('/contact/list', async ctx => {
    let tid = ctx.user.tenantId
    let sql = `
        select * from (
                          select a.id,a.tenantId,a.categoryId,c.name categoryName,a.areaId,b.name areaName,a.salesId,d.name salesName,
                                 a.name,a.linkMobile,a.linkName,a.linkPhone,a.address,a.remark,a.longitude,a.latitude,a.lastBfDay  from contact a left join
                                  area b on a.areaId=b.id left join contact_cat c on a.categoryId=c.id
                                 left join admin d on a.salesId=d.id where a.tenantId=${tid} and a.isDel=0 
                      ) a where tenantId=${tid} 
    `
    let ret = await tool.page(ctx, sql)
    ret.data.list.map(row => {
        row.lastBfDay = row.lastBfDay ? tool.dayDif(tool.dayjs(), row.lastBfDay) : ''
    })
    return ret
})

router.post('/contact/transContact', async ctx => {
    let from = ctx.request.body.formData.from
    let to = ctx.request.body.formData.to
    let tenantId = ctx.user.tenantId
    if(from && to) {
        await db.query(`update contact set salesId=${to} where salesId=${from} and tenantId=${tenantId}`)
    } else {
        return ctx.err('未知业务员')
    }
    ctx.suc('转移成功')
})



module.exports = router;